package com.ruoyi.camp.service.impl;

import java.util.List;

import com.ruoyi.camp.service.IDetailsService;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.camp.mapper.AppointmentMapper;
import com.ruoyi.camp.domain.Appointment;
import com.ruoyi.camp.service.IAppointmentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约Service业务层处理
 * 
 * @author yeluochen
 * @date 2024-11-29
 */
@Service
public class AppointmentServiceImpl implements IAppointmentService 
{
    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired IDetailsService detailsService;

    /**
     * 查询预约
     * 
     * @param atId 预约主键
     * @return 预约
     */
    @Override
    public Appointment selectAppointmentByAtId(String atId)
    {
        return appointmentMapper.selectAppointmentByAtId(atId);
    }

    /**
     * 查询预约列表
     * 
     * @param appointment 预约
     * @return 预约
     */
    @Override
    public List<Appointment> selectAppointmentList(Appointment appointment)
    {
        return appointmentMapper.selectAppointmentList(appointment);
    }

    /**
     * 新增预约
     * 
     * @param appointment 预约
     * @return 结果
     */
    @Override
    @Transactional
    public int insertAppointment(Appointment appointment)
    {
        appointment.setAtId(IdUtils.randomUUID());
        appointment.setAtStatus(0L);
//        将前端传的嵌套数据剥离储存
        for (int i = 0; i < appointment.getDetailsList().size(); i++) {
//            赋值预约ID
            appointment.getDetailsList().get(i).setAtId(appointment.getAtId());
//            赋值预约详情ID
            appointment.getDetailsList().get(i).setDetailId(IdUtils.randomUUID());
            detailsService.insertDetails(appointment.getDetailsList().get(i));
        }

        return appointmentMapper.insertAppointment(appointment);
    }

    /**
     * 修改预约
     *
     * @param appointment 预约
     * @return 结果
     */
    @Override
    public int updateAppointment(Appointment appointment)
    {
        return appointmentMapper.updateAppointment(appointment);
    }

    /**
     * 批量删除预约
     * 
     * @param atIds 需要删除的预约主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByAtIds(Long[] atIds)
    {
        return appointmentMapper.deleteAppointmentByAtIds(atIds);
    }

    /**
     * 删除预约信息
     * 
     * @param atId 预约主键
     * @return 结果
     */
    @Override
    public int deleteAppointmentByAtId(Long atId)
    {
        return appointmentMapper.deleteAppointmentByAtId(atId);
    }
}
